<template>
  <div class="container">
    <div class="partial-title">
      <el-select v-model="select" placeholder="请选择">
        <el-option label="ID" value="id"></el-option>
        <el-option label="类型名称" value="type"></el-option>
      </el-select>
      <el-input
        placeholder="请输入内容"
        v-model="searchField"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getList()"
        ></el-button>
      </el-input>
      <div class="custom-btn" @click="handleAdd()">新增</div>
    </div>
    <el-table :data="tableData" border style="width: 100%; margin-top: 1.75vw">
      <el-table-column fixed prop="id" label="ID"> </el-table-column>
      <el-table-column prop="type" label="类型名称"> </el-table-column>
      <el-table-column prop="createDate" label="加入时间">
        <template slot-scope="scope">
          {{
            scope.row.createDate
              .replace("T", " ")
              .replace("Z", " ")
              .substring(0, 19)
          }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleEditor(scope.row)"
            type="primary"
            size="small"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-size="pageSize"
      :current-page.sync="pageNo"
      :total="total"
      layout="prev, pager, next"
      style="right: 1.5vw; position: absolute"
    >
    </el-pagination>
    <el-dialog title="新增" :visible.sync="addformShow">
      <el-form
        :model="addform"
        :rules="rules"
        ref="addform"
        label-position="left"
      >
        <el-form-item
          label="类型名称"
          :label-width="defaultLabelWidth"
          prop="type"
          maxlength="20"
        >
          <el-input
            v-model="addform.type"
            autocomplete="off"
            style="width: 15vw"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle('addform')">取 消</el-button>
        <el-button type="primary" @click="confirmAdd()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑" :visible.sync="editorformShow">
      <el-form
        :model="editorform"
        :rules="rules"
        ref="editorform"
        label-position="left"
      >
        <el-form-item
          label="类型名称"
          :label-width="defaultLabelWidth"
          prop="type"
          maxlength="20"
        >
          <el-input
            v-model="editorform.type"
            autocomplete="off"
            style="width: 15vw"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle('editorform')">取 消</el-button>
        <el-button type="primary" @click="confirmEditor()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
</style>

<script>
export default {
  data() {
    let baseImgurl = window.location.origin;
    return {
      baseImgurl,
      total: 0,
      pageNo: 1,
      pageSize: 9,
      addformShow: false,
      editorformShow: false,
      select: "id",
      searchField: "",
      defaultLabelWidth: "6vw",
      tableData: [],
      addform: {
        type: "",
      },
      editorform: {
        type: "",
      },
      rules: {
        type: [
          {
            required: true,
            message: "类型名称不能为空！",
            trigger: "blur",
          },
           { validator: this.$validator.typeVal, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    getList() {
      let that = this;
      let query = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      };
      query.role = this.role;
      query[this.select] = this.searchField;
      let queryStr = this.$query.stringify(query);
      console.log(queryStr);
      this.$axios.get(`/api/v1/types?${queryStr}`).then((data) => {
        that.total = data.totalCount;
        that.pageNo = data.pageNo;
        that.tableData = data.types;
      });
    },
    handleAdd() {
      this.addformShow = true;
    },
    handleEditor(row) {
      this.editorform = row;
      this.editorformShow = true;
    },
    handleDelete(row) {
      let that = this;
      this.$axios
        .delete("/api/v1/type?id=" + row.id)
        .then((data) => {
          if (data.result) {
            that.$message.success({
              message: "删除成功",
              center: true,
              offset: screen.availHeight * 0.32,
              duration: 1200,
            });
            that.getList();
          }
        })
        .catch((err) => {
          that.$message.error({
            message: err.data.message,
            center: true,
            offset: screen.availHeight * 0.32,
            duration: 1200,
          });
        });
    },
    cancle(formName) {
      this.$refs[formName].resetFields();
      this.addformShow = false;
      this.editorformShow = false;
    },
    confirmAdd() {
      let that = this;
      this.$refs["addform"].validate((valid) => {
        if (valid) {
          this.addform.role = this.role;
          this.$axios
            .post("/api/v1/type", this.addform)
            .then((data) => {
              if (data.result) {
                that.$message.success({
                  message: "添加成功",
                  center: true,
                  offset: screen.availHeight * 0.32,
                  duration: 1200,
                });
                this.$refs["addform"].resetFields();
                that.getList();
                this.addformShow = false;
              }
            })
            .catch((err) => {
              that.$message.error({
                message: err.data.message,
                center: true,
                offset: screen.availHeight * 0.32,
                duration: 1200,
              });
            });
        }
      });
    },
    confirmEditor() {
      let that = this;
      this.$refs["editorform"].validate((valid) => {
        if (valid) {
          this.$axios
            .put("/api/v1/type", this.editorform)
            .then((data) => {
              if (data.result) {
                that.$message.success({
                  message: "修改成功",
                  center: true,
                  offset: screen.availHeight * 0.32,
                  duration: 1200,
                });
                this.$refs["editorform"].resetFields();
                this.getList();
                this.editorformShow = false;
              }
            })
            .catch((err) => {
              that.$message.error({
                message: err.data.message,
                center: true,
                offset: screen.availHeight * 0.32,
                duration: 1200,
              });
            });
        }
      });
    },
  },
  computed: {},
  beforeMount() {
    this.$axios.post("/api/v1/user/verified");
    this.getList();
  },
};
</script>
